package com.example.echoewish.ui.my_activity.chat_activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.echoewish.R;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.pojo.Chat;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ChatAdapter extends ArrayAdapter<Chat> {
    private static final int MY_LAYOUT= R.layout.my_callback_item;
    private static final int OTHER_LAYOUT=R.layout.others_callback_item;

    private Context context;
    public ChatAdapter(@NonNull Context context, int resource, @NonNull List<Chat> objects) {
        super(context, resource, objects);
        this.context=context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Chat chat=getItem(position);
        if(chat==null)
            return super.getView(position, convertView, parent);
        View view;
        if(chat.id== UserInfo.getInstance().UserId)
        {
            view= LayoutInflater.from(context).inflate(MY_LAYOUT,parent,false);
            CircleImageView circleImageView=view.findViewById(R.id.my_chat_head_image);
            TextView textView=view.findViewById(R.id.my_chat_callback_text_view);
            TextView timeTextView=view.findViewById(R.id.my_callback_time_textview);
            if(UserInfo.getInstance().headImage!=null&&!UserInfo.getInstance().headImage.equals(""))
                Glide.with(view).load(Constant.HEAD_IMAGE_URL+UserInfo.getInstance().headImage).into(circleImageView);
            else
                Glide.with(view).load(R.drawable.no_load).into(circleImageView);
            textView.setText(chat.content);
            timeTextView.setText(chat.time);
        }
        else
        {
            view= LayoutInflater.from(context).inflate(OTHER_LAYOUT,parent,false);
            CircleImageView circleImageView=view.findViewById(R.id.other_chat_head_image);
            TextView textView=view.findViewById(R.id.other_chat_callback_text_view);
            TextView timeTextView=view.findViewById(R.id.other_callback_time_textview);
            if(chat.headImage!=null&&!chat.headImage.equals(""))
                Glide.with(view).load(Constant.HEAD_IMAGE_URL+chat.headImage).into(circleImageView);
            else
                Glide.with(view).load(R.drawable.no_load).into(circleImageView);
            textView.setText(chat.content);
            timeTextView.setText(chat.time);
        }
        return view;
    }
}
